require('./PageTenderplan.styl');
import { Storage } from 'clientConfig/util/StoreData';
import Addpersonfield from 'components/addpersonfield'
const { Group, TextField, SelectField, PhotoField, Icon, Button, DatetimeField, Toast } = SaltUI;
const reactMixin = require('react-mixin');
const Actions = require('./actions');
const Store = require('./store');
const Fileselect = require('components/fileselect')


class Tenderplan extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            upfileName: [],
            approvers: [],//审批人列表
            proName: '' || props.pagedata && props.pagedata.proj_name,
            proNum: '' || props.pagedata && props.pagedata.proj_num,
            budget: ''
        };
        this.data = [
            { label: "年度", name: "annual1" },
            { label: "专项", name: "special" },
            { label: "项目名称", name: "proName" },
            { label: "项目编号", name: "proNum" },
            { label: "预算金额", name: "budget" },
        ]
        if (props.pagedata) {
            let nodelists = props.pagedata.form_component_values.nodelists[0].node_info;
            for (let i = 0; i < this.data.length; i++) {
                if (this.data[i].name) {
                    if (i == 0) {
                        this.state["annual"] = new Date(nodelists[i].value).getTime()//年度必须为this.data的第一个
                        this.state["annual1"] = nodelists[i].value//年度必须为this.data的第一个
                    } else {
                        this.state[this.data[i].name] = nodelists[i].value;
                    }
                }
            }
            this.state.upfileName = JSON.parse(nodelists[nodelists.length - 1].value)//附件信息
            this.state.proName = props.pagedata.proj_name
            this.state.proNum = props.pagedata.proj_num
            this.state.edit = props.edit;
        }
    }
    handleTextChange(name, newValue) {
        this.setState({
            [name]: newValue
        });
    }

    handleTimeChange(name, newValue) {
        var time_data = new Date(newValue).toLocaleDateString();
        if (time_data.indexOf('年') > -1) {
            time_data = time_data.split('年')[0]
        } else {
            time_data = time_data.split('/')[0]
        }
        this.setState({
            [name]: newValue,
            [name + '1']: time_data,
        });
    }
    //提交按钮
    handleClick() {
        if (!this.state.proName) {
            Toast.show({
                type: 'error',
                content: `项目名称不能为空`
            });
            return;
        } else if (!this.state.proNum) {
            Toast.show({
                type: 'error',
                content: `项目编号不能为空`
            });
            return;
        } else if (!this.state.annual) {
            Toast.show({
                type: 'error',
                content: `年度不能为空`
            });
            return;
        } else if (!this.state.special) {
            Toast.show({
                type: 'error',
                content: `专项不能为空`
            });
            return;
        } else if (!this.state.budget) {
            Toast.show({
                type: 'error',
                content: `预算不能为空`
            });
            return;
        } else if (!this.refs.fileselect.fileData || this.refs.fileselect.fileData.length == 0) {
            Toast.show({
                type: 'error',
                content: `附件不能为空`
            });
            return;
        } else if (!this.state['approvers'] || this.state['approvers'].length == 0) {
            Toast.show({
                type: 'error',
                content: `审批人不能为空`
            });
            return;
        }
        if (this.props.pagedata) {
            Actions.submit({ node: this.data, data: this.state, upfileName: this.refs.fileselect.fileData, process_id: this.process_id, edit: this.state.edit, pagedata: this.props.pagedata });
        } else {
            Actions.submit({ node: this.data, data: this.state, upfileName: this.refs.fileselect.fileData, process_id: this.process_id });
        }
    }

    handleAddpersonCb(names) {
        this.setState({ approvers: names })
    }
    handleCCpersonCb(names) {
        this.setState({ copyers: names })
    }
    handleAddpersonUnionid(unionids) {
        this.setState({
            approvers_id: unionids
        })
    }
    handlecsrUnionid(unionids) {
        this.setState({
            cc_list_id: unionids
        })
    }

    render() {
        let t = this;
        return (
            <div className="tenderplan myform">
                <Group.List >
                    <TextField
                        label="项目名称"
                        value={t.state.proName}
                        onChange={t.handleTextChange.bind(t, 'proName')}
                        readOnly={t.props.edit}
                    />
                    <TextField
                        label="项目编号"
                        value={t.state.proNum}
                        onChange={t.handleTextChange.bind(t, 'proNum')}
                        readOnly={t.props.edit}
                    />
                </Group.List>
                <Group.List >
                    <DatetimeField
                        label="年度"
                        placeholder="请选择"
                        columns={['年']}
                        className="time"
                        value={this.state.annual}
                        onSelect={this.handleTimeChange.bind(t, 'annual')}
                        readOnly={t.props.edit}
                    />
                </Group.List>

                <Group.List >
                    <TextField
                        label="专项"
                        placeholder="请输入专项名称"
                        value={t.state.special}
                        onChange={t.handleTextChange.bind(t, 'special')}
                        readOnly={t.props.edit}
                    />
                </Group.List>
                <Group.List >
                    <TextField
                        className="suffix"
                        label="预算金额"
                        placeholder="请输入预算金额"
                        value={t.state.budget.replace(/万元/, '')}
                        onChange={t.handleTextChange.bind(t, 'budget')}
                        readOnly={t.props.edit}
                    />
                </Group.List>
                <Fileselect
                    ref='fileselect'
                    fold={true}
                    version={true}
                    upfileName={this.state.upfileName}
                    proName={this.state.proName}
                    edit={this.props.edit}
                    folder_id={0}
                    pathdata=''
                    path_name=''
                    message="tender-tenderplan"
                />
                <Group.List>
                    <Addpersonfield
                        label="发送给：甲方"
                        cb={t.handleAddpersonCb.bind(t)}
                        union={t.handleAddpersonUnionid.bind(t)}
                        approvers={t.state.approvers}
                        approvers_id={t.state.approvers_id}
                        maxLength="10" />
                </Group.List>
                <Group.List>
                    <Addpersonfield
                        label="抄送人"
                        cb={t.handleCCpersonCb.bind(t)}
                        union={t.handlecsrUnionid.bind(t)}
                        maxLength="10"
                        approvers={t.state.cc_list}
                        approvers_id={t.state.cc_list_id}
                    />
                </Group.List>
                <Button type="primary" className="submitBtn" onClick={t.handleClick.bind(t)}>提交</Button>
            </div>
        );
    }

    componentWillMount() {
    }

    componentDidMount() {
    }

    componentWillReceiveProps(nextProps) {
        this.setState({ edit: nextProps.edit })
        if (nextProps.pagedata) {
            if (nextProps.pagedata.approvers && nextProps.pagedata.approvers instanceof Array)
                this.setState({
                    approvers: nextProps.pagedata.approvers,
                    approvers_id: nextProps.pagedata.approvers_id,
                });
            else if (nextProps.pagedata.approvers && typeof nextProps.pagedata.approvers == 'string')
                this.setState({
                    approvers: [nextProps.pagedata.approvers],
                    approvers_id: [nextProps.pagedata.approvers_id]
                });
            if (nextProps.pagedata.cc_list && nextProps.pagedata.cc_list instanceof Array)
                this.setState({
                    cc_list: nextProps.pagedata.cc_list,
                    cc_list_id: nextProps.pagedata.cc_list_id
                });
            else if (nextProps.pagedata.cc_list && typeof nextProps.pagedata.cc_list == 'string')
                this.setState({
                    cc_list: [nextProps.pagedata.cc_list],
                    cc_list_id: [nextProps.pagedata.cc_list_id]
                })
        }
    }

    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }

    componentWillUpdate(nextProps, nextState) {
    }

    componentDidUpdate(prevProps, prevState) {
    }

    componentWillUnmount() {
    }
}

reactMixin.onClass(Tenderplan, Reflux.connect(Store));

module.exports = Tenderplan;
